// admin/system/handler/sys_job_handler.go
package handler

import (
	"go-admin1/admin/system/model"
	"go-admin1/admin/system/service"
	"go-admin1/internal/common/utils"

	"github.com/gin-gonic/gin"

	"strconv"
)

// SysJobHandler 定时任务处理器
type SysJobHandler struct {
	jobService service.SysJobService
}

// NewSysJobHandler 创建定时任务处理器实例
func NewSysJobHandler(jobService service.SysJobService) *SysJobHandler {
	return &SysJobHandler{
		jobService: jobService,
	}
}

// Create 创建定时任务
func (h *SysJobHandler) Create(c *gin.Context) {
	var job model.SysJob
	if err := c.ShouldBindJSON(&job); err != nil {
		//	response.Error(c, http.StatusBadRequest, "参数解析失败: "+err.Error())
		return
	}

	if err := h.jobService.Create(&job); err != nil {
		//response.Error(c, http.StatusInternalServerError, "创建定时任务失败: "+err.Error())
		return
	}

	//	response.Success(c, "创建定时任务成功", job)
}

// GetByID 根据ID获取定时任务
func (h *SysJobHandler) GetByID(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		//		response.Error(c, http.StatusBadRequest, "参数解析失败: "+err.Error())
		return
	}

	job, err := h.jobService.GetByID(id)
	if err != nil {
		//		response.Error(c, http.StatusInternalServerError, "获取定时任务失败: "+err.Error())
		return
	}
	utils.OkResponse(c, job)
	//		response.Success(c, "获取定时任务成功", job)
}

// Update 更新定时任务
func (h *SysJobHandler) Update(c *gin.Context) {
	var job model.SysJob
	if err := c.ShouldBindJSON(&job); err != nil {
		//		response.Error(c, http.StatusBadRequest, "参数解析失败: "+err.Error())
		return
	}

	if err := h.jobService.Update(&job); err != nil {
		//		response.Error(c, http.StatusInternalServerError, "更新定时任务失败: "+err.Error())
		return
	}

	//		response.Success(c, "更新定时任务成功", job)
}

// Delete 删除定时任务
func (h *SysJobHandler) Delete(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		//			response.Error(c, http.StatusBadRequest, "参数解析失败: "+err.Error())
		return
	}

	if err := h.jobService.Delete(id); err != nil {
		//			response.Error(c, http.StatusInternalServerError, "删除定时任务失败: "+err.Error())
		return
	}

	//		response.Success(c, "删除定时任务成功", nil)
}

// List 定时任务列表
func (h *SysJobHandler) List(c *gin.Context) {
	//page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	//pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	//
	////jobs, total, err := h.jobService.List(page, pageSize)
	//if err != nil {
	//	//		response.Error(c, http.StatusInternalServerError, "获取定时任务列表失败: "+err.Error())
	//	return
	//}

	//response.Success(c, "获取定时任务列表成功", gin.H{
	//	"list":     jobs,
	//	"total":    total,
	//	"page":     page,
	//	"pageSize": pageSize,
	//})
}

// ListAll 所有定时任务列表
func (h *SysJobHandler) ListAll(c *gin.Context) {
	jobs, err := h.jobService.ListAll()
	if err != nil {
		//	response.Error(c, http.StatusInternalServerError, "获取定时任务列表失败: "+err.Error())
		return
	}
	utils.OkResponse(c, jobs)
	//	response.Success(c, "获取定时任务列表成功", jobs)
}

// ListByStatus 根据状态获取定时任务列表
func (h *SysJobHandler) ListByStatus(c *gin.Context) {
	status := c.Query("status")
	if status == "" {
		//		response.Error(c, http.StatusBadRequest, "状态不能为空")
		return
	}

	jobs, err := h.jobService.ListByStatus(status)
	if err != nil {
		//		response.Error(c, http.StatusInternalServerError, "获取定时任务列表失败: "+err.Error())
		return
	}
	resultData := map[string]interface{}{
		"list":  jobs,
		"total": 1,
	}
	utils.OkResponse(c, resultData)
	//		response.Success(c, "获取定时任务列表成功", jobs)
}
